package cooperate

import "gddgame.cc/galaxy/common/namespaces/cooperate/structure"

type CheckIntMode byte

const (
	EqualCheckIntMode = iota
	GreaterCheckIntMode
	LessCheckIntMode
)

type CheckSetMode byte

const (
	ExistCheckSetMode = iota
	NoExistCheckSetMode
)

type CheckSortedMode byte

const (
	ExistCheckSortedMode = iota
	NoExistCheckSortedMode
	GreaterCheckSortedMode
	LessCheckSortedMode
)

type CheckBuilder struct {
	execute *LogicExecute
	mapNoId string

	part     []CheckData
	all      [][]CheckData
	current  CheckData
	contents []CheckContent
}

func (logic *LogicExecute) CheckBuilder() *CheckBuilder {
	return &CheckBuilder{
		execute: logic,
	}
}

func (builder *CheckBuilder) BindMapNoId(mapNoId string) *CheckBuilder {
	builder.mapNoId = mapNoId
	return builder
}
func (builder *CheckBuilder) And(dataType structure.PublicDataType, id interface{}) *CheckBuilder {
	builder.UpdateAnd()
	builder.current = CheckData{
		Data: structure.BaseData{DataType: dataType, Id: id},
	}
	return builder
}

func (builder *CheckBuilder) Exist() *CheckBuilder {
	content := CheckContent{
		Check: ExistCheck,
	}
	builder.contents = append(builder.contents, content)
	return builder
}

func (builder *CheckBuilder) DataInt(key string, value int) *CheckBuilder {
	content := CheckContent{
		Check: DataCheck,
		Key:   key,
		Value: value,
	}
	builder.contents = append(builder.contents, content)
	return builder
}

func (builder *CheckBuilder) DataMode(key string, value int, mode CheckIntMode) *CheckBuilder {
	content := CheckContent{
		Check: DataCheck,
		Key:   key,
		Value: value,
	}
	switch mode {
	case EqualCheckIntMode:
		content.Check = EqualCheck
	case GreaterCheckIntMode:
		content.Check = GreaterCheck
	case LessCheckIntMode:
		content.Check = LessCheck
	}
	builder.contents = append(builder.contents, content)
	return builder
}

func (builder *CheckBuilder) Position(key string, position []byte, equal bool) *CheckBuilder {
	content := CheckContent{
		Key:   key,
		Value: position,
	}
	if equal {
		content.Check = PositionEqualCheck
	} else {
		content.Check = PositionUnEqualCheck
	}
	builder.contents = append(builder.contents, content)
	return builder
}

//
//func (builder *CheckBuilder) List(extend string, index int, value interface{}, mode CheckIntMode) *CheckBuilder {
//	content := CheckContent{
//		Key:   extend,
//		Index: index,
//		Value: value,
//	}
//	switch mode {
//	case EqualCheckIntMode:
//		content.Check = ListEqualCheck
//	case GreaterCheckIntMode:
//		content.Check = ListGreaterCheck
//	case LessCheckIntMode:
//		content.Check = ListLessCheck
//	}
//	builder.contents = append(builder.contents, content)
//	return builder
//}
//func (builder *CheckBuilder) Set(extend string, value interface{}, mode CheckSetMode) *CheckBuilder {
//	content := CheckContent{
//		Key:   extend,
//		Value: value,
//	}
//	switch mode {
//	case ExistCheckSetMode:
//		content.Check = SetExistCheck
//	case NoExistCheckSetMode:
//		content.Check = SetNoExistCheck
//	}
//	builder.contents = append(builder.contents, content)
//	return builder
//}
//
//func (builder *CheckBuilder) Sorted(extend string, index int, value interface{}, mode CheckSortedMode) *CheckBuilder {
//	content := CheckContent{
//		Key:   extend,
//		Index: index,
//		Value: value,
//	}
//	switch mode {
//	case ExistCheckSortedMode:
//		content.Check = SetExistCheck
//	case NoExistCheckSortedMode:
//		content.Check = SetNoExistCheck
//	case GreaterCheckSortedMode:
//		content.Check = SortedGreaterCheck
//	case LessCheckSortedMode:
//		content.Check = SortedLessCheck
//	}
//	builder.contents = append(builder.contents, content)
//	return builder
//}

func (builder *CheckBuilder) UpdateOr() {
	if len(builder.part) > 0 {
		builder.all = append(builder.all, builder.part)
		builder.part = builder.part[0:0]
	}
}

func (builder *CheckBuilder) UpdateAnd() {
	if builder.contents != nil && len(builder.contents) > 0 {
		builder.current.Checks = builder.contents
		builder.part = append(builder.part, builder.current)
		builder.contents = builder.contents[0:0]
	}
}
func (builder *CheckBuilder) Or() *CheckBuilder {
	builder.UpdateAnd()
	builder.UpdateOr()
	return builder
}

func (builder *CheckBuilder) Data() [][]CheckData {
	builder.UpdateAnd()
	builder.UpdateOr()

	data := builder.all
	//Release(this)
	return data
}
